package com.dayaokeji.imkitwrapper.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.beetle.bauhinia.gallery.GalleryImage;
import com.beetle.bauhinia.gallery.tool.DisplayUtils;
import com.beetle.bauhinia.gallery.ui.GalleryGridAdapter;
import com.beetle.bauhinia.gallery.ui.GalleryGridUI;
import com.beetle.bauhinia.gallery.ui.GalleryUI;
import com.dayaokeji.imkitwrapper.R;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class GalleryGridFragment extends BaseFragment {

    private static final String INTENT_EXTRA_KEY_IMAGES = "images";
    private static final String INTENT_EXTRA_KEY_POSITION = "position";

    private ArrayList<GalleryImage> imagesList;

    private GridView gridView;
    private GalleryGridAdapter mGridAdapter;

    private int initialPosition = 0;
    private onNavigationToGalleryListener onNavigationToGalleryListener;


    public static GalleryGridFragment newInstance(ArrayList<GalleryImage> imagesList,int position) {
        
        Bundle args = new Bundle();
        args.putInt(INTENT_EXTRA_KEY_POSITION, position);
        args.putParcelableArrayList(INTENT_EXTRA_KEY_IMAGES, imagesList);
        GalleryGridFragment fragment = new GalleryGridFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_gallery_grid, container, false);
        gridView = (GridView) rootView.findViewById(com.beetle.imkit.R.id.child_grid);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initialPosition = getArguments().getInt(INTENT_EXTRA_KEY_POSITION, 0);
        imagesList = getArguments().getParcelableArrayList(INTENT_EXTRA_KEY_IMAGES);
        
        initView();
    }

    private void initView() {
        mGridAdapter = new GalleryGridAdapter(getActivity(), imagesList, DisplayUtils.getScreenWidth(getActivity()));
        gridView.setAdapter(mGridAdapter);
        gridView.setSelection(initialPosition);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (onNavigationToGalleryListener != null) {
                    onNavigationToGalleryListener.onNavigationGallery(imagesList, position);
                }
            }
        });
    }

    private void navigateToViewImageDetail(int position) {
//        Intent intent = GalleryUI.getCallingIntent(this, imagesList, position, true);
//        startActivity(intent);
    }

    public void setOnNavigationToGalleryListener(GalleryGridFragment.onNavigationToGalleryListener onNavigationToGalleryListener) {
        this.onNavigationToGalleryListener = onNavigationToGalleryListener;
    }

    public interface onNavigationToGalleryListener{
        void onNavigationGallery(ArrayList<GalleryImage> imagesList,int position);
    }
}
